package com.lastproject.ejb.entities;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name="role")
public class Role implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7910731187125977890L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	
	private String name;
	
	@ManyToMany
	@JoinTable(name = "role_permission",
	        joinColumns = {@JoinColumn(name = "idRole")},
	        inverseJoinColumns = {@JoinColumn(name = "idPermission")})
	private List<Permission> permissions;
	
//	@ManyToMany
//	@JoinTable(name = "role_users",
//	        joinColumns = {@JoinColumn(name = "idRole")},
//	        inverseJoinColumns = {@JoinColumn(name = "idUser")})
	@ManyToMany(mappedBy = "roles")
	private List<User> users;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}
	
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

//	@Override
//	public int hashCode() {
//		final int prime = 31;
//		int result = 1;
//		result = prime * result + id;
//		result = prime * result + ((name == null) ? 0 : name.hashCode());
//		result = prime * result
//				+ ((permissions == null) ? 0 : permissions.hashCode());
//		result = prime * result + ((users == null) ? 0 : users.hashCode());
//		return result;
//	}
//
//	@Override
//	public boolean equals(Object obj) {
//		if (this == obj)
//			return true;
//		if (obj == null)
//			return false;
//		if (getClass() != obj.getClass())
//			return false;
//		Role other = (Role) obj;
//		if (id != other.id)
//			return false;
//		if (name == null) {
//			if (other.name != null)
//				return false;
//		} else if (!name.equals(other.name))
//			return false;
//		if (permissions == null) {
//			if (other.permissions != null)
//				return false;
//		} else if (!permissions.equals(other.permissions))
//			return false;
//		if (users == null) {
//			if (other.users != null)
//				return false;
//		} else if (!users.equals(other.users))
//			return false;
//		return true;
//	}
//
//	@Override
//	public String toString() {
//		return "Role [id=" + id + ", name=" + name + ", permissions="
//				+ permissions + ", users=" + users + "]";
//	}
	
}
